package com.zmx.job.execute.core;

import com.zmx.core.client.auth.StartClient;
import com.zmx.core.executor.JobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: 初始化客户端连接认证 定时发送心跳
 * @ClassName: InitClient
 * @Author zhaomxr
 * @Date 2021-12-15 14:29
 */
@Slf4j
@Configuration
public class InitClient {

    @Value("${auth.username}")
    private String username;

    @Value("${auth.password}")
    private String password;

    @Value("${auth.secret}")
    private String secret;

    @Value("${server.port}")
    private int port;

    @Value("${auth.url}")
    private String authUrl;

    // 认证重试间隔时间
    @Value("${auth.retry.time:10}")
    private int authRetryTime;

    // 认证 心跳监测 重试间隔时间，默认60秒
    @Value("${auth.heart.retry.time:60}")
    private int authHeartRetryTime;

    @Value("${job.max.meanwhile}")
    private int maxMeanwhile;

    @Value("${job.listener.port:9999}")
    private int jobListenerPort;

    /**
     * 注册当前实例到服务端（仅用于注册当前实例，维护自身节点有效性）
     * @return
     */
    @Bean("client")
    public StartClient initClient() {
        return new StartClient(jobListenerPort, username, password, secret, authUrl, authRetryTime, authHeartRetryTime);
    }

    /**
     * 启用自身实例所有表明@MallJob注解任务方法，开启自身监听服务端发来执行任务通道，反射执行具体任务
     * @return
     */
    @Bean
    public JobSpringExecutor jobSpringExecutor() {
        JobSpringExecutor jobSpringExecutor = new JobSpringExecutor();
        jobSpringExecutor.setUsername(username);
        jobSpringExecutor.setPassword(password);
        jobSpringExecutor.setSecret(secret);
        jobSpringExecutor.setPort(jobListenerPort);
        jobSpringExecutor.setMaxMeanwhile(maxMeanwhile);
        return jobSpringExecutor;
    }
}
